#include "mainscene.h"
#include "item/imageitem.h"
#include "recource.h"
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>
#include "sound/GameSounds.h"
#include <QSettings>
#include <QLocale>
#include <QEvent>

enum MainBtn
{
    SEL_MB,
    HELP_MB,
    OVI_MB,
    QUIT_MB,
    ABOUT_MB,
    NULL_MB,
}mBtn(NULL_MB);

MainScene::MainScene(QObject *parent) :
    QGraphicsScene(parent)
{
    setSceneRect(0.0, 0.0, 360.0, 640.0);

    initItem();
}

void MainScene::initItem()
{
    switch(Recource::m_iLanguage)
    {
    case QLocale::Chinese:
    {
        m_bgItem = new ImageItem(*Recource::m_mainBgCnImage,
                                 QRect(QPoint(0,0),Recource::m_mainBgCnImage->size()));
        m_selectItem = new ImageItem(*Recource::m_mainPlayCnImage,
                                    QRect(QPoint(0,0),Recource::m_mainPlayCnImage->size()));
        m_quitItem = new ImageItem(*Recource::m_mainExitCnImage,
                                    QRect(QPoint(0,0),Recource::m_mainExitCnImage->size()));
    }
        break;
//    case QLocale::HongKong:
    case 255:
    {
        m_bgItem = new ImageItem(*Recource::m_mainBgHkImage,
                                 QRect(QPoint(0,0),Recource::m_mainBgHkImage->size()));
        m_selectItem = new ImageItem(*Recource::m_mainPlayHkImage,
                                    QRect(QPoint(0,0),Recource::m_mainPlayHkImage->size()));
        m_quitItem = new ImageItem(*Recource::m_mainExitCnImage,
                                    QRect(QPoint(0,0),Recource::m_mainExitCnImage->size()));
    }
        break;
    default:
    {
        m_bgItem = new ImageItem(*Recource::m_mainBgEnImage,
                                 QRect(QPoint(0,0),Recource::m_mainBgEnImage->size()));
        m_selectItem = new ImageItem(*Recource::m_mainPlayEnImage,
                                    QRect(QPoint(0,0),Recource::m_mainPlayEnImage->size()));
        m_quitItem = new ImageItem(*Recource::m_mainExitEnImage,
                                    QRect(QPoint(0,0),Recource::m_mainExitEnImage->size()));
    }
        break;
    }

    m_bgItem->setPos(0.0,0.0);
    m_bgItem->setZValue(0.0);
    addItem(m_bgItem);

    m_startItem = new ImageItem(*Recource::m_soundCloseImage,
                                QRect(QPoint(0,0),Recource::m_soundCloseImage->size()));
    if(Recource::m_bPlaySound)
       m_startItem->setImage(*Recource::m_soundOpenImage);
    else
       m_startItem->setImage(*Recource::m_soundCloseImage);
    m_startItem->setZValue(1.0);
    m_startItem->setPos(270.0,0.0);
    addItem(m_startItem);

    m_selectItem->setZValue(1.0);
    m_selectItem->setPos(67.0,389.0);
    m_selectItem->setVisible(false);
    addItem(m_selectItem);

    m_helpItem = new ImageItem(QImage(":/image/main/help.png"),
                                QRect(QPoint(0,0),QImage(":/image/main/help.png").size()));
    m_helpItem->setZValue(1.0);
    m_helpItem->setPos(0.0,560.0);
    m_helpItem->setVisible(false);
    addItem(m_helpItem);

    m_oviItem = new ImageItem(QImage(":/image/main/ovi.png"),
                                QRect(QPoint(0,0),QImage(":/image/main/ovi.png").size()));
    m_oviItem->setZValue(1.0);
    m_oviItem->setPos(0.0,0.0);
    m_oviItem->setVisible(false);
    addItem(m_oviItem);

    m_quitItem->setZValue(1.0);
    m_quitItem->setPos(67.0,479.0);
    m_quitItem->setVisible(false);
    addItem(m_quitItem);

    m_aboutItem = new ImageItem(QImage(":/image/main/about.png"),
                                QRect(QPoint(0,0),QImage(":/image/main/about.png").size()));
    m_aboutItem->setZValue(1.0);
    m_aboutItem->setPos(280.0,560.0);
    m_aboutItem->setVisible(false);
    addItem(m_aboutItem);   
}

void MainScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(m_startItem->isUnderMouse())
    {
        Recource::m_bPlaySound = !Recource::m_bPlaySound;
        QSettings settings("MySoft", "Score");
        settings.setValue("main/sound",Recource::m_bPlaySound);
        if(Recource::m_bPlaySound)
           m_startItem->setImage(*Recource::m_soundOpenImage);
        else
           m_startItem->setImage(*Recource::m_soundCloseImage);

        if(Recource::m_bPlaySound)
        {
            Recource::m_gamesound->playBgSound(0);
        }
        else
        {
            Recource::m_gamesound->stop(0);
        }
    }
    else if(m_selectItem->isUnderMouse())
    {
        m_selectItem->setVisible(true);
        mBtn = SEL_MB;

        if(Recource::m_bPlaySound)
        {
            Recource::m_gamesound->play(0);
        }
    }
    else if(m_helpItem->isUnderMouse())
    {
        m_helpItem->setVisible(true);
        mBtn = HELP_MB;

        if(Recource::m_bPlaySound)
        {
            Recource::m_gamesound->play(0);
        }
    }
    else if(m_oviItem->isUnderMouse())
    {
        m_oviItem->setVisible(true);
        mBtn = OVI_MB;

        if(Recource::m_bPlaySound)
        {
            Recource::m_gamesound->play(0);
        }
    }
    else if(m_quitItem->isUnderMouse())
    {
        m_quitItem->setVisible(true);
        mBtn = QUIT_MB;

        if(Recource::m_bPlaySound)
        {
            Recource::m_gamesound->play(0);
        }
    }
    else if(m_aboutItem->isUnderMouse())
    {
        m_aboutItem->setVisible(true);
       mBtn = ABOUT_MB;

       if(Recource::m_bPlaySound)
       {
           Recource::m_gamesound->play(0);
       }
    }
}

void MainScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    switch(mBtn)
    {
    case SEL_MB:
    {
        m_selectItem->setVisible(false);
        if(m_selectItem->isUnderMouse())
        {
           Recource::SetSel();
        }
    }
        break;
    case HELP_MB:
    {
        if(Recource::m_bPlaySound)
        {
            Recource::m_gamesound->play(0);
        }

        m_helpItem->setVisible(false);
        if(m_helpItem->isUnderMouse())
        {
            Recource::SetHelp();
        }
    }
        break;
    case OVI_MB:
    {
        m_oviItem->setVisible(false);
        if(m_oviItem->isUnderMouse())
        {
            if(!QDesktopServices::openUrl(QUrl("http://store.ovi.mobi",QUrl::TolerantMode)))
            {

                QMessageBox msgBox;

                msgBox.setText("Fail to connect Ovi,please check network connections!");

                msgBox.setStandardButtons(QMessageBox::Yes);
                msgBox.setDefaultButton(QMessageBox::Yes);
                msgBox.exec();
            }
        }
    }
        break;
    case QUIT_MB:
    {
        m_quitItem->setVisible(false);
        if(m_quitItem->isUnderMouse())
        {
            emit showPrompt();
        }
    }
        break;
    case ABOUT_MB:
    {
        m_aboutItem->setVisible(false);
        if(m_aboutItem->isUnderMouse())
        {
            Recource::SetSet();
        }
    }
        break;
    default:
        break;
    }
}

